<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Wallet extends MY_Controller {

	public $left_menu_min;

	function __construct(){
	parent::__construct();

	  $parent_id = 9;
	  //获取school二级菜单
	  $this->load->model('School_manage_menu_model');
	  $menu_list = $this->School_manage_menu_model->get_menu_list($this->is_manage,$parent_id);
	  // print_r($menu_list);exit;
	  $this->smarty->assign("menu_list", $menu_list);

	  $this->left_menu_min = array("menu" => "营销中心", "curr" => $_SERVER['REQUEST_URI'], "list" => $menu_list);
	  $this->smarty->assign("left_menu_min", $this->left_menu_min);

	  $this->smarty->assign("menu", 'wallet');

	}


	/**
	 * Index Page for this controller.
	 */
	public function index($nick = 0, $mobile = 0, $limit=0)
	{
		$post_data = $this->input->post();
		if($post_data){
			if(!empty($post_data['nick'])){
				$nick = $post_data['nick'];
			}
			if(!empty($post_data['mobile'])){
				$mobile = $post_data['mobile'];
			}
		}
		$conditions['nick'] = $nick;
		$conditions['mobile'] = $mobile;
		$conditions['school_id'] = $this->user_data['school_id'];
		//获取会员list
		$users = $this->User_model->get_users_list($limit,$this->offset,$conditions);
		$this->smarty->assign("users", $users['result']);

		$this->load->library('pagination');
		$page_config['base_url'] = '/wallet/index/'.$nick.'/'.$mobile;
		$page_config['total_rows'] = $users['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 5;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();

		$this->smarty->assign("pagination", $pagination);
		// $this->smarty->assign("user_data", $this->user_data);
		$this->smarty->display("wallet/index.html");
	}

	function tixian($status = 'all', $limit =0){
		// $this->smarty->assign("user_data", $this->user_data);
		$this->load->model('User_withdrawal_model');
		$result = $this->User_withdrawal_model->get_withdrawl_by_school($status,$this->user_data['school_id'],$limit,$this->offset);
		$this->smarty->assign("withdrawal", $result['result']);

		$this->load->library('pagination');
		$page_config['base_url'] = "/wallet/tixian/".$status.'/';
		$page_config['total_rows'] = $result['total_count'];
		$page_config['per_page'] = $this->offset;
		$page_config['uri_segment'] = 4;
		$this->pagination->initialize($page_config);
		$pagination = $this->pagination->create_links();
		$this->smarty->assign("pagination", $pagination);
		$this->smarty->assign("status", $status);

		$this->smarty->display("wallet/tixian.html");
	}

	function payment($id = 0){
		// $this->smarty->assign("user_data", $this->user_data);

		if(empty($id)){
			show_message("/wallet/tixian", "非法操作");exit;
		}
		$this->load->model('User_withdrawal_model');
		$tixian_info = $this->User_withdrawal_model->get_tixian_info($id);

		if(!$tixian_info){
			show_message("/wallet/tixian", "数据不存在");exit;
		}
		if($tixian_info['school_id'] != $this->user_data['school_id']){
			show_message("/wallet/tixian", "无法处理该提现申请");exit;
		}
		if(empty($tixian_info['open_id'])){
			show_message("/wallet/tixian", "该用户没有绑定微信帐号，无法提现");exit;
		}

		if($_POST){
			if($tixian_info['balance'] < $tixian_info['amount']){
				show_message("/wallet/tixian", "用户余额不足");exit;
			}
			$post_data = $this->input->post();
			require_once 'third_party/wxpayApi/wxpay.php';
			$wx_config_data = get_field_by_where("o2o_school_wx_config", "*", "school_id='{$this->user_data['school_id']}'");
			if(!$wx_config_data || empty($wx_config_data['app_id']) || empty($wx_config_data['mch_id']) || empty($wx_config_data['apikey']) || empty($wx_config_data['apiclient_cert']) || empty($wx_config_data['apiclient_key'])){
				show_message("/user/orders", "该学校不支持微信支付");exit;
			}
			// print_r($wx_config_data);
			$wx_config['mch_appid'] = $wx_config_data['app_id'];
			$wx_config['mchid'] = $wx_config_data['mch_id'];
			$wx_config['apikey'] = $wx_config_data['apikey'];
			$wx_config['sslcertPath'] = getcwd().$wx_config_data['apiclient_cert'];
			$wx_config['sslkeyPath'] = getcwd().$wx_config_data['apiclient_key'];
			/*$wx_config['sslcertPath'] = getcwd().'/third_party/wxpayApi/cert/apiclient_cert.pem';
			$wx_config['sslkeyPath'] = getcwd().'/third_party/wxpayApi/cert/apiclient_key.pem';*/
			// $wx_config['rootcaPath'] = '/third_party/wxpayApi/cert/rootca.pem';
			$weixin_pay = new wxpay($wx_config);
			
			$params['partner_trade_no'] = date("YmdHis", time()) . $this->user_data['school_id'] . $id;
			$params['amount'] = $tixian_info['amount'] * 100;
			$params['openid'] = $tixian_info['open_id'];
			$params['desc'] = $post_data['desc'];
			$response = $weixin_pay->transfers($params);
			
			if(isset($response['return_code']) && $response['return_code'] == 'SUCCESS'){
				if(isset($response['result_code']) && $response['result_code'] == 'SUCCESS'){
					//修改提现申请状态
					$this->db->where("id", $id);
					$this->db->update("user_withdrawal", array("desc"=>$post_data['desc'], "pay_time"=>strtotime($response['payment_time']), "payment_no" => $response['payment_no'], "teacher_id" => $this->user_data['teacher_id'], "teacher_name" => $this->user_data['user_name'], "status" => 2) );
					
					//修改账户余额
					$this->db->where("user_id", $tixian_info['user_id']);
					$this->db->set("balance", "balance - {$tixian_info['amount']}", false);
     				$this->db->update("user_account");
					show_message("/wallet/tixian", "提现处理成功");exit;
				}else{
					if($response['err_code_des'] == '余额不足') $response['err_code_des'] = '微信账户'.$response['err_code_des'];
					show_message("/wallet/payment/".$id, $response['err_code_des']);exit;
				}
			}else{
				show_message("/wallet/payment/".$id, "提现处理失败，请重试");exit;
			}
		}

		$this->smarty->assign("tixian_info", $tixian_info);

		$this->smarty->display("wallet/payment.html");
	}

	function audit_ajax($id,$status){
		$this->load->model('User_withdrawal_model');
		$this->User_withdrawal_model->audit($id,$status);

		print_r('1');exit;
	}
}
